package cn.fayang.ymcc.service.impl;

import cn.fayang.ymcc.constant.util.AssertUtil;
import cn.fayang.ymcc.domain.KillActivity;
import cn.fayang.ymcc.domain.KillCourse;
import cn.fayang.ymcc.dto.ActivityDto;
import cn.fayang.ymcc.mapper.KillActivityMapper;
import cn.fayang.ymcc.service.IKillActivityService;
import cn.fayang.ymcc.service.IKillCourseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoxiao
 * @since 2022-08-06
 */
@Service
public class KillActivityServiceImpl extends ServiceImpl<KillActivityMapper, KillActivity> implements IKillActivityService {
    @Autowired
    private IKillCourseService killCourseService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;
    /**
     * 新增秒杀活动
     * @param dto
     */
    @Override
    public void save(ActivityDto dto) {
        AssertUtil.isNotNull(dto,"参数不能为空");
        KillActivity killActivity = new KillActivity();
        killActivity.setEndTime(dto.getEndTime());
        killActivity.setStartTime(dto.getStartTime());
        killActivity.setPublishStatus(KillActivity.KILL_WAIT);
        killActivity.setPublishTime(new Date());
        insert(killActivity);
    }

    /**
     * 秒杀活动上架
     * @param activityID
     */
    @Override
    public void onlive(Long activityID) {
        AssertUtil.isNotNull(activityID,"非法请求");
        Wrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("activity_id",activityID);
        //查询出秒杀活动下的所有秒杀课程
        List<KillCourse> killCourses = killCourseService.selectList(wrapper);
        KillCourse killCourse = killCourses.get(0);
        Date startTime = killCourse.getStartTime();
        Date now = new Date();
        //只要活动的结束时间没有开始就可以发布
        boolean before = now.before(killCourse.getEndTime());
        AssertUtil.isTrue(before,"活动时间不合法，发布失败！！");
        killCourses.forEach(course->{
            // 设置key
            String key = activityID +":"+course.getId();
            // 获取信号量
            RSemaphore semaphore = redissonClient.getSemaphore(key);
            //将信号量设置为秒杀课程数量  返回boolean值 true为成功 失败为false
            boolean setPermits = semaphore.trySetPermits(course.getKillCount());
            if(setPermits){
                // 储存大key 加前缀
                redisTemplate.opsForHash().put("activity:"+activityID,course.getId().toString(),course);
                //修改秒杀课程状态
                course.setPublishTime(now);
                course.setPublishStatus(1);
                killCourseService.updateById(course);
            }else{
            }
        });
        KillActivity killActivity = selectById(activityID);
        killActivity.setPublishTime(now);
        killActivity.setPublishStatus(1);
        updateById(killActivity);


    }


}
